package com.interauto.Controller.casepage;

import com.interauto.bean.web.InterinfoWeb;
import com.interauto.bean.web.InterruntWeb;
import com.interauto.service.AutoDebugRunInterService;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.safety.Whitelist;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "/run")
public class RunHome {
    private static final Logger logger= LoggerFactory.getLogger(RunHome.class);

    @Autowired
    private AutoDebugRunInterService autoDebugRunInterService;

    @RequestMapping(value = "/debuginter",method = RequestMethod.POST)
    public ModelAndView rundebughome(@RequestBody InterinfoWeb interinfoWeb){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("runinterinfo");
        String url=interinfoWeb.getHttpurl();
        InterruntWeb interruntWeb=new InterruntWeb();
        modelAndView.addObject("interruntWeb",interruntWeb);
        if(StringUtils.isEmpty(url)){
            interruntWeb.setStatus(1);
            interruntWeb.setMessage("请求URL为空");
            return modelAndView;
        }
        interruntWeb=autoDebugRunInterService.debugruninterinfo(interinfoWeb);
        String resinfo=interruntWeb.getResinfo();
        Whitelist whitelist = Whitelist.none();
        if(StringUtils.isNoneEmpty(interruntWeb.getMessage())){
            //System.out.println("-->"+Jsoup.clean(interruntWeb.getMessage(),whitelist));
            interruntWeb.setMessage(Jsoup.clean(interruntWeb.getMessage(),whitelist));
        }
        if(StringUtils.isNoneEmpty(resinfo)){  //处理返回的内容可能有xss问题
            interruntWeb.setResinfo(Jsoup.clean(resinfo,whitelist));
        }
        modelAndView.addObject("interruntWeb",interruntWeb);
        return modelAndView;
    }

    @RequestMapping(value = "/debugpage",method = RequestMethod.GET)
    public String rundebugpagehome(){
        return "debugpage";
    }
}
